package com.be.beadmin.device.controller;

import com.be.beadmin.device.entity.ToolFile;
import com.be.beadmin.device.dto.output.ToolFileEx;
import com.be.beadmin.project.Util.MinioUtil;
import com.be.beadmin.service.impl.MinioService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.service.ToolFileService;
import com.be.beadmin.device.dto.input.ToolFileQueryCriteria;
import com.be.beadmin.device.dto.input.ToolFileDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author sly
* @date 2023-10-18
**/
@RestController
public class ToolFileController {

    @Autowired
    private  ToolFileService toolFileService;
    @Resource
    private MinioService minioService;

    MinioUtil minioUtil = new MinioUtil();

    private String bucketName = "toolfile";

    @PostMapping(value = "/device/getToolFileByPage")
    public ResultModel getToolFileByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(toolFileService.getToolFileByPage(pageEntity)) ;
    }

    @PostMapping("/device/addToolFile")
    public ResultModel addToolFile(@RequestBody ToolFileDto toolFileDto){
        toolFileService.addToolFile(toolFileDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delToolFile")
    public ResultModel delToolFile(@RequestBody ToolFileDto toolFileDto){
        toolFileService.delToolFile(toolFileDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateToolFile")
    public ResultModel updateToolFile(@RequestBody ToolFileDto toolFileDto){
        toolFileService.updateToolFile(toolFileDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportToolFile")
    public void exportToolFile(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ToolFileEx> toolFilePage = toolFileService.getToolFileByPage(pageEntity);
        List<ToolFileEx> toolFileExes = toolFilePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ToolFile接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ToolFileEx.class).sheet("sheet1").doWrite(toolFileExes);
    }

    @ApiOperation("新增附件信息")
    @PostMapping("/device/uploadToolFile")
    public ResultModel uploadToolFile(
            @ApiParam(value = "附件",required = true) @RequestParam("file") MultipartFile[] multipartFiles,
            @ApiParam(value = "toolinfo_id",required = true) @RequestParam String toolinfoId,
            @ApiParam(value = "文件名称") @RequestParam String fileName,
            @ApiParam(value = "备注") @RequestParam(required = false) String memo) throws Exception {

        toolFileService.uploadToolFile(toolinfoId,multipartFiles,fileName,memo,bucketName);
        return ResultModel.success("添加附件成功");
    }
}